﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;

namespace Core.SimpleTemp.Common
{
    /// <summary>
    /// 此方法后期有优化控件 ，根据自定义主键完成不同转换
    /// 可以采用工厂或者建造者模式
    /// </summary>
    public static class HttpContextAccessorExtensions
    {
        /// <summary>
        /// 获取当前登录账号ID
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="httpContextAccessor"></param>
        /// <returns></returns>
        public static Guid CurrentUserId(this IHttpContextAccessor httpContextAccessor)
        {
            Guid userId = Guid.Empty; ;
            var sidClaim = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Sid);
            if (sidClaim != null)
            {
                var id = sidClaim.Value;
                Guid guid;
                if (Guid.TryParse(id, out guid))
                    userId = guid;
            }

            return userId;
        }
    }
}
